package org.manager.properties;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;
import org.manager.generic.Repository.GenericHibernateRepository;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository("propertyRepository")
public class PropertyRepositoryImpl extends GenericHibernateRepository<Property> implements PropertyRepository {

	public List<Property> findPageProperty(final Property property, final int start, final int limit) {
		
		List<Property> propertyList =
		getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createCriteria(Property.class).add(Example.create(property)).setFirstResult(start*limit).setMaxResults(limit).list();
			}
		});
		return propertyList;
	}
	
	public int totalRecord(final Property property) {
		return (Integer)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createCriteria(Property.class)
				.add(Example.create(property))
				.setProjection(Projections.rowCount())
				.uniqueResult();
			}
		});
	}
	
	
	public List<Property> findPropertyAndPropertiesCount(final Serializable id){
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List<Property> l = new ArrayList<Property>();
				l = session.createQuery("from Property where id = :id")
					.setString("id", (String)id)
					.list();
				
				
				return l; 
			}
		});
	}
	
	
}
